﻿function insertSubscribeButton() {
	var finalURL = getVideoURL();
	checkSubscribed(finalURL);
}

/**
    Check current video is subscribed or not.
	then continue to initialize UI interface
*/
function checkSubscribed(url) {
    var key = extractKeyFromURL(url + "");
	var _videoIndex = "";
	chrome.extension.sendRequest({
		action: "getVideoIndex",
	}, function(response) {
		_videoIndex = response.videoIndex;
		console.log("VideoIndex:" + _videoIndex);
		var isSubed = true;
		if(_videoIndex.indexOf(key) == -1) {
			isSubed = false;;
		}
		console.log("subed? ->" + isSubed);
		initUI(isSubed);
	});
}


/**
Initialize UI interface.
Add two images and with click function.

*/
function initUI(isSubed) {
	var finalURL = getVideoURL();
	var $href = $("#bdhd");
	
	var $subImg = $("<img src='http://www.easyicon.cn/download/png/34379/16/'/><span>订阅此视频</span>");
	var $cancelSubImg = $("<img src='http://www.easyicon.cn/download/png/521626/16/'/><span>取消订阅</span>");
	$href.append($subImg);
	$href.append($cancelSubImg);
	
	$subImg.click(function(){
		chrome.extension.sendRequest({
			action: "createRecord",
			name: getVideoName(),
			state: getVideoState(),
			updateTime: getVideoUpdateTime(),
			url: finalURL,
			type: getVideoType()
		}, function(response){
			if(response.status == "done") {
				$subImg.hide();
				$cancelSubImg.show();
				
				chrome.extension.sendRequest({
					action: "storeStaticInfo",
					name: key + "_detail",
					note: getVideoNote(),
					lang: getVideoLang(),
					logo: getVideoLogo(),
					score: getVideoScore()
				}, function(response){
					if(response.status == "done")
						console.log("store static info OK. key = " + key);
					else
						console.log("store static info Error. key = " + key);
				});
				
			} else {
				alert("Unknow error");
			}
		});
	});	
	
	var key = extractKeyFromURL(getVideoURL() + "");
	$cancelSubImg.click(function(){
		chrome.extension.sendRequest({
			action: "deleteRecord",
			name: key
		}, function(response) {
		    console.log("cancel response");
			$subImg.show();
			$cancelSubImg.hide();
			
			chrome.extension.sendRequest({
				action: "deleteStaticInfo",
				name: key + "_detail"
			}, function(response){
				if(response.status == "done")
					console.log("delete static info OK. key = " + key);
				else
					console.log("delete static info Error. key = " + key);
			});
			
		});
	});
	
	if(isSubed) {
	    $subImg.hide();
		$cancelSubImg.show();
	} else {
	    $subImg.show();
		$cancelSubImg.hide();
	}
	
}

/*-----------------------Video Attr Methods----------------------------*/
function getVideoName(){
	$name = $(".de_text  h1").html();
	console.log("name:" + $name);
	return $name;
}
function getVideoNote(){
	$note = $(".note").html();
	console.log("note:"+$note);
	return $note;
}
function getVideoLang(){
	$lang = $(".videonote p").html();
	console.log("lang:"+$lang);
	return $lang;
}
function getVideoState(){
	$state = $(".de_text > ul > li:lt(1) > p").html();
	console.log("state:"+$state);
	return $state;
}
function getVideoScore(){
	$score = $(".order_score").html();
	console.log("score:"+$score);
	return $score;
}
function getVideoLogo(){
	$logo = $(".de_img a img").attr("src");
	console.log("logo:"+$logo);
	return $logo;
}
function getVideoType(){
	$type = $(".sub_nav p a:eq(1)").html();
	console.log("type:"+$type);
	return $type;
}
function getVideoUpdateTime(){
	$updateTime = $(".videofixtime").html();
	//$updateTime = $updateTime.subString(5,10);
	//var value = $updateTime.text();
	$value = $updateTime.substring(5, 10).trim();
	console.log("updateTime:"+$updateTime);
	console.log("value:"+$value);
	return $value;
}
function getVideoURL() {
	$url = $(".desktop > a").attr("href");
	var startStr = "urlstr=";
	var endStr = "&urlname=";
	var startIndex = $url.indexOf(startStr);
	var endIndex = $url.indexOf(endStr);
	var startStrLen = startStr.length;
	var finalURL = $url.substring(startIndex + startStrLen, endIndex);
	return finalURL;
}

/*---------------------Portal------------------------*/
insertSubscribeButton();